package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.PageInfo;
import net.xju.common.utils.Result;
import net.xju.entity.User;
import net.xju.service.UserService;
import net.xju.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/user")
public class UserController {
    UserService userService = new UserServiceImpl();

    // 获取当前登录用户
    @RequestMapping("/userInfo.do")
    public void userInfo(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        User user = (User) request.getSession().getAttribute("user");
        JSONUtil.printByJSON(response, Result.success(user));
    }

    // 登录
    @RequestMapping("/login.do")
    public void login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user =  userService.login(username,password); // alt+enter
        if (user != null) {
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            JSONUtil.printByJSON(resp, Result.success(user, "登录成功"));
        } else {
            JSONUtil.printByJSON(resp,Result.fail("账号或者密码有误"));
        }
    }
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setEmail(req.getParameter("email"));
        user.setRole(Integer.parseInt(req.getParameter("role")));
        user.setGender(req.getParameter("gender"));
        user.setAvatar(req.getParameter("avatar"));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.insert(user);
        JSONUtil.printByJSON(resp, Result.success("添加成功"));
    }
    // 根据id更新用户
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        User user = new User();
        user.setId(Integer.parseInt(req.getParameter("id")));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setEmail(req.getParameter("email"));
        user.setRole(Integer.parseInt(req.getParameter("role")));
        user.setGender(req.getParameter("gender"));
        user.setAvatar(req.getParameter("avatar"));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        if (userService.update(user)) {
            JSONUtil.printByJSON(resp, Result.success("更新成功"));
        } else {
            JSONUtil.printByJSON(resp,Result.fail("更新失败"));
        }
    }

    // 根据id查询用户
    @RequestMapping("/selectById.do")
    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        User user = userService.selectById(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success(user));
    }

    // 根据id 删除用户
    @RequestMapping("/deleteById.do")
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        if (userService.deleteById(Integer.parseInt(id))) {
            JSONUtil.printByJSON(resp, Result.success("删除成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.success("删除失败"));
        }
    }

    // 条件分页查询
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) {
        // 控制层 -》业务层-》持久层
        // 条件查询 username  gender
        String username = req.getParameter("username");
        String gender = req.getParameter("gender");
        // 分页参数
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null&& pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr == null && pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        PageInfo<User> pageInfo = userService.pageList(username,gender,pageNo,pageSize);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));

    }
}
